﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UserControls_ShoppingCartl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetLVData();
        }
    }

    void GetLVData()
    {
        lvShoppingCart.DataSource = Profile.grpShoppingCart.MyCart.Items;
        lvShoppingCart.DataBind();
        DisplayTotal();
    }

    private void DisplayTotal()
    {
        Label lblTotalCount = lvShoppingCart.FindControl("lblTotalCount") as Label;
        Label lblTotalPrice = lvShoppingCart.FindControl("lblTotalPrice") as Label;
        if (lblTotalCount == null || lblTotalPrice == null)
        {
            return;
        }
        decimal[] total = Profile.grpShoppingCart.MyCart.GetTotal();
        lblTotalPrice.Text = total[0].ToString();
        lblTotalCount.Text = total[1].ToString();
    }
 
    protected void lvShoppingCart_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "PrdtDel":
                {
                    Profile.grpShoppingCart.MyCart.RemoveItem(e.CommandArgument.ToString());
                    GetLVData();
                } break;
            case "PrdtUpdate":
                {
                    UpdateCart();
                    DisplayTotal();
                } break;
            case "PrdtBuy":
                {
                    UpdateCart();
                    if (Profile.IsAnonymous)
                    {
                        Response.Redirect("~/Login.aspx?path=ShoppingCart.aspx");
                    }
                    Response.Redirect("~/Member/MyUserInfo.aspx");
                } break;
            case "ConBuy":
                {
                    UpdateCart();
                    Response.Redirect("Index.aspx");
                } break;
            default:break;
        }
    }

    private void UpdateCart()
    {
        for (int i = 0; i < this.lvShoppingCart.Items.Count; i++)
        {
            LinkButton btnPrdtName = this.lvShoppingCart.Items[i].FindControl("btnPrdtName") as LinkButton;
            TextBox txtBox = this.lvShoppingCart.Items[i].FindControl("txtCount") as TextBox;
            Profile.grpShoppingCart.MyCart.UpdateCart(btnPrdtName.CommandArgument, Convert.ToInt32(txtBox.Text));

        }
    }
   
    protected void lvShoppingCart_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (lvShoppingCart.FindControl("btnBuy") != null)
        {
            if (!Profile.IsAnonymous)
            {
                IList<GoShopping.Model.AddressList> iAl = GetAddressLists();
                if (iAl.Count == 0)
                {
                    ((LinkButton)(lvShoppingCart.FindControl("btnBuy"))).Attributes.Add("onclick", "return confirm('您的详细信息不完整,补充完整？')");
                }
                else
                {
                    foreach (GoShopping.Model.AddressList al in iAl)
                    {
                        if (al.ProvinceID == 0 || al.CityID == 0 || al.CountyID == 0 || al.DetailStreet == null || al.AcceptPeople == null || al.AcceptPeopleTel == null || al.UserID == 0 || al.ZipCode == null)
                        {
                            ((LinkButton)(lvShoppingCart.FindControl("btnBuy"))).Attributes.Add("onclick", "return confirm('您的详细信息不完整,补充完整？')");
                            break;
                        }
                    }
                }
            }
            else
            {
                ((LinkButton)(lvShoppingCart.FindControl("btnBuy"))).Attributes.Add("onclick", "return confirm('你还没有登录,要登录吗？')");
            }
        }
        ListViewItem lvi = e.Item;
        LinkButton btnDel = lvi.FindControl("btnDel") as LinkButton;
        if (btnDel != null)
        {
            btnDel.Attributes.Add("onclick", "return confirm('确定删除吗?')");
        }
    }
    private IList<GoShopping.Model.AddressList> GetAddressLists()
    {
        try
        {
            GoShopping.BLL.UserBean ub = new GoShopping.BLL.UserBean();
            GoShopping.Model.UserInfo ui = ub.GetOneUser(Context.User.Identity.Name);
            GoShopping.BLL.ProfileInfo.AddressListBean alb = new GoShopping.BLL.ProfileInfo.AddressListBean();
            IList<GoShopping.Model.AddressList> iAl = alb.GetUserAddressList(ui.ID);
            return iAl;
        }
        catch
        {
            Response.Redirect("~/Error.aspx");
        }
        return null;
    }
}
